package com.weiyi.mashangzhu.Weight.recyclerview.listener;

import android.view.View;

import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.Utils.TimeFormat;

public class ListenerWithPosition implements View.OnClickListener, View.OnLongClickListener {

    private int mPosition;
    private Object mHolder;
    private OnClickWithPositionListener mOnClickListener;
    private OnLongClickWithPositionListener mOnLongClickListener;
    public static final int MIN_CLICK_DELAY_TIME = 2000;
    private long lastClickTime = 0;

    public ListenerWithPosition(int position, Object holder) {
        this.mPosition = position;
        this.mHolder = holder;
    }

    @Override
    public void onClick(View v) {
        if (mOnClickListener != null) {
            long currentTime = TimeFormat.getSystemMilliSecond();
            if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
                lastClickTime = currentTime;
                mOnClickListener.onClick(v, mPosition, mHolder);
            } else {
                T.showShort(MyApp.getAppContext(), R.string.click_not_frequent);
            }
        }
    }

    public interface OnClickWithPositionListener<T> {
        void onClick(View v, int position, T holder);
    }

    public void setOnClickListener(OnClickWithPositionListener mOnClickListener) {
        this.mOnClickListener = mOnClickListener;
    }

    @Override
    public boolean onLongClick(View v) {
        if (mOnLongClickListener != null) {
            mOnLongClickListener.onLongClick(v, mPosition, mHolder);
        }
        return true;
    }

    public interface OnLongClickWithPositionListener<T> {
        void onLongClick(View v, int position, T holder);
    }

    public void setOnLongClickListener(OnLongClickWithPositionListener mOnLongClickListener) {
        this.mOnLongClickListener = mOnLongClickListener;
    }
}
